package edu.hnu.tradeserver.common.util;

import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * @author 21g
 * @version 1.0
 * @className TimeUtil
 * @since 1.0
 */
public class TimeUtil {

    // 计算起始时间
    public static LocalDate calculateStartDate(String timeRange, LocalDate endDate) {
        LocalDate startDate;
        switch (timeRange) {
            case "1D":
                startDate = endDate.minusDays(1);
                break;
            case "1W":
                startDate = endDate.minusWeeks(1);
                break;
            case "2W":
                startDate = endDate.minusWeeks(2);
                break;
            case "1M":
                startDate = endDate.minusMonths(1);
                break;
            case "3M":
                startDate = endDate.minusMonths(3);
                break;
            case "6M":
                startDate = endDate.minusMonths(6);
                break;
            case "1Y":
                startDate = endDate.minusYears(1);
                break;
            case "YTD":
                int currentYear = endDate.getYear();
                startDate = LocalDate.of(currentYear, 1, 1);
                break;
            default:
                // 默认使用一周的时间范围
                startDate = endDate.minusWeeks(1);
                break;
        }
        return startDate;
    }

}
